using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using dal = PConsulting.DataAccess;
using PConsulting.DataSets;

namespace PConsulting.Business
{
    [Serializable]
    public class Project
    {
        #region Properties
        private int projectId;

        public int ProjectId
        {
            get { return projectId; }
            set { projectId = value; }
        }
        private string client;

        public string Client
        {
            get { return client; }
            set { client = value; }
        }
        private DateTime startDate;

        public DateTime StartDate
        {
            get { return startDate; }
            set { startDate = value; }
        }
        private DateTime endDate;

        public DateTime EndDate
        {
            get { return endDate; }
            set { endDate = value; }
        }
        private string accountManager;

        public string AccountManager
        {
            get { return accountManager; }
            set { accountManager = value; }
        }
        private string projectResponsible;

        public string ProjectResponsible
        {
            get { return projectResponsible; }
            set { projectResponsible = value; }
        }
        #endregion

        public Project()
        {
        }

        public tdsMonths SelectMonths(string userName)
        {
            dal.Project pr = new dal.Project();

            DataSet ds = pr.SelectMonths(userName);
            tdsMonths months = new tdsMonths();
            DateTime minMonth = (DateTime)ds.Tables[0].Rows[0]["minmonth"];
            DateTime maxMonth = (DateTime)ds.Tables[0].Rows[0]["maxmonth"];

            months.tblMonths.AddtblMonthsRow(minMonth.Month.ToString() + " " + minMonth.Year.ToString(), new DateTime(minMonth.Year, minMonth.Month, 1));
            while (minMonth <= maxMonth)
            {
                minMonth = minMonth.AddMonths(1);
                months.tblMonths.AddtblMonthsRow(GetMonthName(minMonth.Month) + " " + minMonth.Year.ToString(), new DateTime(minMonth.Year, minMonth.Month, 1));
            }
            return months;
        }

        public tdsMonths SelectMonths()
        {
            dal.Project pr = new dal.Project();

            DataSet ds = pr.SelectMonths();
            tdsMonths months = new tdsMonths();
            DateTime minMonth = (DateTime)ds.Tables[0].Rows[0]["minmonth"];
            DateTime maxMonth = (DateTime)ds.Tables[0].Rows[0]["maxmonth"];

            months.tblMonths.AddtblMonthsRow(minMonth.Month.ToString() + " " + minMonth.Year.ToString(), new DateTime(minMonth.Year, minMonth.Month, 1));
            while (minMonth <= maxMonth)
            {
                minMonth = minMonth.AddMonths(1);
                months.tblMonths.AddtblMonthsRow(GetMonthName(minMonth.Month) + " " + minMonth.Year.ToString(), new DateTime(minMonth.Year, minMonth.Month, 1));
            }
            return months;
        }

        public Project SelectById(int projectId)
        {
            dal.Project pr = new dal.Project();

            DataSet ds = pr.SelectById(projectId);
            Project proj = new Project();
            if (ds.Tables[0].Rows.Count > 0)
                return FillObject(ds.Tables[0].Rows[0]);
            else return null;
        }
 
        public List<Project> SelectByUserName(string userName)
        {
            dal.Project pr = new dal.Project();

            DataSet ds = pr.SelectByUserName(userName);
            List<Project> projects = new List<Project>();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                projects.Add(FillObject(row));
            }
            projects.Sort(new ProjectComparer());
            return projects;
        }

        public List<Project> SelectByUserNameMonth(string userName, DateTime month)
        {
            dal.Project pr = new dal.Project();

            DataSet ds = pr.SelectByUserNameMonth(userName, month);
            List<Project> projects = new List<Project>();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                projects.Add(FillObject(row));
            }
            return projects;
        }

        private Project FillObject(DataRow row)
        {
            Project pr = new Project();
            pr.projectId = (int)row["projectId"];
            pr.client = (string)row["client"];
            pr.startDate = (DateTime)row["startDate"];
            pr.endDate = (DateTime)row["endDate"];
            pr.accountManager = (string)row["accountManager"];
            pr.projectResponsible = (string)row["projectResponsible"];
            return pr;
        }

        private string GetMonthName(int month)
        {
            switch (month)
            {
                case 1:
                    return "January";
                case 2:
                    return "February";
                case 3:
                    return "March";
                case 4:
                    return "April";
                case 5:
                    return "May";
                case 6:
                    return "June";
                case 7: 
                    return "July";
                case 8:
                    return "August";
                case 9: 
                    return "September";
                case 10:
                    return "October";
                case 11:
                    return "November";
                case 12:
                    return "December";
            }
            return "";
        }

        public void Update(string userName)
        {
            dal.Project pr = new dal.Project();
            if (projectId > 0)
                pr.Update(projectId, client, startDate, endDate, accountManager, projectResponsible);
            else
                pr.Insert(userName, client, startDate, endDate, accountManager, projectResponsible);
        }

        public void Delete()
        {
            dal.Project pr = new dal.Project();
            pr.Delete(projectId);
        }
    }

    public class ProjectComparer : IComparer<Project>
    {
        #region IComparer<Project> Members

        public int Compare(Project x, Project y)
        {
            return y.StartDate.CompareTo(x.StartDate);
        }

        #endregion
    }
}